
package com.wyzz.hr.bo.impl;

import java.util.*;
import org.hibernate.FetchMode;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.IOutmatchBasicBO;
import com.wyzz.hr.dao.IOutmatchBasicDAO;
import com.wyzz.hr.pojo.Iodef;
import com.wyzz.hr.pojo.Outmatch;
import com.wyzz.hr.pojo.OutmatchBasic;


public class OutmatchBasicBOImpl implements IOutmatchBasicBO {

	public OutmatchBasicBOImpl() {
	}

	public IOutmatchBasicDAO getDao() {
		return dao;
	}

	public void setDao(IOutmatchBasicDAO dao) {
		this.dao = dao;
	}

	public OutmatchBasic loadObject(String id) {
		return loadObject(id, ((String[]) (null)));
	}

	public OutmatchBasic loadObject(String id, String fetch[]) {
		return (OutmatchBasic) dao
				.loadObject(com.wyzz.hr.pojo.OutmatchBasic.class, id, fetch,
						new boolean[0]);
	}

	public OutmatchBasic loadObject(Iodef iodef, String name) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.OutmatchBasic.class);
		detachedCriteria.add(Restrictions.eq(OutmatchBasic.PROP_OMB_IO, iodef));
		detachedCriteria.add(Restrictions.eq(OutmatchBasic.PROP_OMB_FIELD_NAME,
				name));
		List result = dao.findByCriteria(detachedCriteria);
		if (result != null && result.size() != 1)
			return null;
		else
			return (OutmatchBasic) result.get(0);
	}

	public boolean saveObject(OutmatchBasic obj) {
		dao.saveObject(obj);
		return true;
	}

	public boolean saveOrUpdateObject(OutmatchBasic omb) {
		dao.saveOrupdate(omb);
		if (omb.getOmList() != null)
			dao.saveOrupdate(omb.getOmList());
		return true;
	}

	public boolean updateObject(OutmatchBasic obj) {
		dao.updateObject(obj);
		return true;
	}

	public boolean deleteObject(OutmatchBasic omb) {
		if (omb.getOmList() != null)
			dao.deleteCollection(omb.getOmList());
		dao.deleteObject(omb);
		return true;
	}

	public List getListByIodef(Iodef iodef) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.OutmatchBasic.class);
		detachedCriteria.add(Restrictions.eq(OutmatchBasic.PROP_OMB_IO, iodef));
		detachedCriteria.addOrder(Order.asc(OutmatchBasic.PROP_OMB_SORT_ID));
		List result = dao.findByCriteria(detachedCriteria);
		return result;
	}

	public List createOutmatch(Iodef iodef) {
		List ombList = getListByIodef(iodef);
		List result = new ArrayList();
		for (int i = 0; i < ombList.size(); i++) {
			Outmatch omTmp = new Outmatch();
			OutmatchBasic ombTmp = (OutmatchBasic) ombList.get(i);
			omTmp.initByOutmatchBasic(ombTmp);
			omTmp.setAllMaps();
			result.add(omTmp);
		}

		return result;
	}

	public void initOutmatchBasicMaps(List ombList) {
		for (int i = 0; i < ombList.size(); i++)
			((OutmatchBasic) ombList.get(i)).setAllMaps();

	}

	public boolean updateOmbOmList(List ombList, List omList) {
		if (ombList != null && ombList.size() > 0)
			dao.saveOrupdate(ombList);
		if (omList != null && omList.size() > 0)
			dao.saveOrupdate(omList);
		return true;
	}

	public List getOmbListByFieldNameArr(String ioName, String fieldNameArr[]) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.OutmatchBasic.class);
		if (fieldNameArr != null)
			detachedCriteria.add(Restrictions.in(
					OutmatchBasic.PROP_OMB_FIELD_NAME, fieldNameArr));
		else
			detachedCriteria.add(Restrictions.like(
					OutmatchBasic.PROP_OMB_FIELD_NAME, "outPutList.%"));
		detachedCriteria
				.setFetchMode(OutmatchBasic.PROP_OMB_IO, FetchMode.JOIN);
		detachedCriteria.createAlias(OutmatchBasic.PROP_OMB_IO, "ombIo");
		detachedCriteria.add(Restrictions.eq("ombIo.ioName", ioName));
		List result = dao.findByCriteria(detachedCriteria);
		return result;
	}

	public String getOmbListByFieldName(String fieldName) {
		String updateOmbHql;
		if (fieldName != null)
			updateOmbHql = (new StringBuilder())
					.append(
							"select ombId from OutmatchBasic where ombFieldName='outPutList.")
					.append(fieldName).append("'").toString();
		else
			updateOmbHql = "select ombId from OutmatchBasic where ombFieldName like 'outPutList.%'";
		List ombIdList = dao.exeHqlList(updateOmbHql);
		if (ombIdList == null || ombIdList.size() == 0)
			return null;
		StringBuffer OmbIds_b = new StringBuffer("");
		String ombId;
		for (Iterator i$ = ombIdList.iterator(); i$.hasNext(); OmbIds_b.append(
				"'").append(ombId).append("',"))
			ombId = (String) i$.next();

		String omBIds = OmbIds_b.toString().substring(0, OmbIds_b.length() - 1);
		return omBIds;
	}

	private IOutmatchBasicDAO dao;
}
